package com.typ.member.config;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


/**
 * @author heran
 * @version 1.0, 2019/2/2 16:12
 */
@Component
@Aspect
@Slf4j
public class ServiceLogAop {
    @Pointcut("execution(public * com.typ.member.web.api..*.*(..))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Exception {
        try {
            log.info("request {} {}", proceedingJoinPoint.getTarget().getClass().getName() + "#" + proceedingJoinPoint.getSignature().getName(), JSON.toJSONString(proceedingJoinPoint.getArgs()));
            Object obj = proceedingJoinPoint.proceed();
            log.info("response {}", JSON.toJSONString(obj));
            return obj;
        } catch (Exception e) {
            throw e;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
